package com.bristua.mall.admin.user.biz;

import com.bristua.mall.admin.common.bean.PageBean;
import com.bristua.mall.admin.user.dao.TUserMapper;
import com.bristua.mall.admin.user.model.TUser;
import com.bristua.mall.admin.user.model.condition.QueryUsersParams;
import com.bristua.mall.admin.user.model.condition.TUserExample;
import com.bristua.mall.admin.user.model.vo.QuerUsersVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/4/2.
 */
@Service
public class QueryUsersBiz {

    @Autowired
    private TUserMapper tUserMapper;

    public PageBean queryUserBiz(QueryUsersParams queryUsersParams){
        TUserExample tUserExample = new TUserExample();
        TUserExample.Criteria criteria = tUserExample.createCriteria();
        if(!StringUtils.isEmpty(queryUsersParams.getUserId())){
            criteria.andUserIdLike("%"+queryUsersParams.getUserId()+"%");
        }

        if(!StringUtils.isEmpty(queryUsersParams.getUserName())){
            criteria.andUserNameLike("%"+queryUsersParams.getUserName()+"%");
        }
        Page<TUser> objects = PageHelper.startPage(queryUsersParams.getPageNo(),queryUsersParams.getPageSize());
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        PageBean data = new PageBean<>(objects);
        if(data.getList()!=null && data.getList().size()>0){
            List<QuerUsersVO> querUsersVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TUser tUser = (TUser) data.getList().get(i);
                QuerUsersVO querUsersVO = new QuerUsersVO();
                BeanUtils.copyProperties(tUser,querUsersVO);
                querUsersVOS.add(querUsersVO);
            }
            data.getList().clear();
            data.getList().addAll(querUsersVOS);
        }
        return data;
    }
}
